﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Xaml;

using SolutionOrchestrator.Properties;

namespace SolutionOrchestrator
{
	[ContentProperty("RootPathCollection")]
	public class MySettings : ObservableObject
	{
		public MySettings()
		{
			_rootPathCollection.CollectionChanged += RootPathCollectionChanged;
		}

		void RootPathCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			Save();
		}

		protected override void NpcPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			Save();
		}

		ISourceControl _sourceControl = new CommandLineSourceControl();

		public ISourceControl SourceControl
		{
			get
			{
				return _sourceControl;
			}
			set
			{
				if (_sourceControl != value && value != null)
				{
					Unsubscribe(_sourceControl);
					_sourceControl = value;
					Subscribe(_sourceControl);
					OnPropertyChanged("SourceControl");
				}
			}
		}

		readonly ObservableCollection<string> _rootPathCollection = new ObservableCollection<string>();

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public IList<string> RootPathCollection
		{
			get { return _rootPathCollection; }
		}

		string _currentRootPath;

		public string CurrentRootPath
		{
			get { return _currentRootPath; }
			set
			{
				_currentRootPath = value;
				OnPropertyChanged("CurrentRootPath");
			}
		}

		protected override void OnPropertyChanged(string name)
		{
			Save();
			base.OnPropertyChanged(name);
		}

		void Save()
		{
			Settings.Default.MySettings = XamlServices.Save(this);
			Settings.Default.Save();
		}

	}
}
